﻿using BroadGoalComponents.BGControls;
using BroadGoalComponents.BGControls.Toast;
using BroadGoalComponents.Dialogs;
using Custom.Controls;
using Custom.Framework;
using Custom.Model.Entity;
using Custom.Service.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Custom.Business.Custom
{
    public partial class frmCustomAdd : PopupFormWithCover
    {
        public frmCustomAdd()
        {
            InitializeComponent();

            this.btnSave.Focus();
            this.btnSave.Select();
        }

        private void frmCustomAdd_Load(object sender, EventArgs e)
        {
            this.bindLevelData();
            this.initData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.saveData();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 更改身份证号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtIdentityNo_TextChanged(object sender, EventArgs e)
        {
            BGTextBox txt = this.txtIdentityNo;

            if (txt.Text.Trim().Length >= 14)
            {
                this.txtBirthday.Text = txt.Text.Trim().Substring(6, 4) + "-" + txt.Text.Trim().Substring(10, 2) + "-" + txt.Text.Trim().Substring(12, 2);
            }
            else
            {
                this.txtBirthday.Text = "";
            }
        }

        /// <summary>
        /// 绑定会员等级数据
        /// </summary>
        private void bindLevelData()
        {
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
            list.Add(new KeyValuePair<string, string>("1", "一级"));
            list.Add(new KeyValuePair<string, string>("2", "二级"));
            this.ddlLevel.DataSource = list.ToArray();
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        protected virtual void initData()
        { }

        /// <summary>
        /// 保存会员数据
        /// </summary>
        protected virtual void saveData()
        {
            if (!this.checkData()) return;

            CustomInfo c = new CustomInfo()
            {
                cardno = this.txtCardNo.Text.Trim(),
                level = this.ddlLevel.SelectedValue,
                cusname = this.txtName.Text.Trim(),
                sex = this.rbMan.Checked ? "0" : "1",
                identityno = this.txtIdentityNo.Text.Trim(),
                birthday = this.txtBirthday.Text.Trim(),
                address = this.txtAddress.Text.Trim(),
                mobile = this.txtMobile.Text.Trim(),
                qq = this.txtQQ.Text.Trim(),
                email = this.txtEmail.Text.Trim(),
                memo = this.txtMemo.Text.Trim(),
                createman = "",
                createtime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
            };

            try
            {
                new CustomInfoRepository().Insert(c);

                BGToast.Show("保存成功");

                this.Close();
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                WarningDialog.Show("保存出错啦");
            }
        }

        /// <summary>
        /// 检查会员数据
        /// </summary>
        /// <returns></returns>
        protected virtual bool checkData()
        {
            if (string.IsNullOrEmpty(this.txtCardNo.Text.Trim()))
            {
                WarningDialog.Show(this, "请输入会员卡号");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                WarningDialog.Show(this, "请输入会员姓名");
                return false;
            }

            List<CustomInfo> customs = new CustomInfoRepository().Get(w => w.cardno == this.txtCardNo.Text.Trim());
            if (customs != null && customs.Count() > 0)
            {
                WarningDialog.Show(this, "已存在相同的会员卡号，请重新输入");
                return false;
            }

            return true;
        }

    }
}
